# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=openxr-sdk
pkgbase=mingw-w64-${_realname}
pkgname=${MINGW_PACKAGE_PREFIX}-${_realname}
pkgver=1.0.9
pkgrel=3
pkgdesc='OpenXR is a royalty-free, open standard that provides high-performance access to Augmented Reality (AR) and Virtual Reality (VR) (mingw-w64)'
arch=('any')
url='https://www.khronos.org/registry/cl/'
license=('Apache 2.0')
depends=("${MINGW_PACKAGE_PREFIX}-jsoncpp"
         "${MINGW_PACKAGE_PREFIX}-vulkan-loader")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-python")
source=(${_realname}-${pkgver}.tar.gz::https://github.com/KhronosGroup/OpenXR-SDK/archive/release-${pkgver}.tar.gz
        001-generate-pkgconfig-files.patch
        002-libname-def-file.patch)
sha256sums=('fb6f9141723f34678f3aadb4d6d91ff03559c86eccc2bf2987495c9d45cfab25'
            '8af59e72237185982c3234ceebf98f61b57bc4ac39f51ec619c04b2120bceb51'
            'f564371aed6dc7bfc2aaad3745791fa00ccb8f7a346da2aca82e3ee780fd7a9c')

prepare() {
  cd ${srcdir}/OpenXR-SDK-release-${pkgver}
  patch -p1 -i ${srcdir}/001-generate-pkgconfig-files.patch
  patch -p1 -i ${srcdir}/002-libname-def-file.patch
}

build() {
  [[ -d "${srcdir}"/build-${MINGW_CHOST} ]] && rm -rf ${srcdir}/build-${MINGW_CHOST}
  mkdir "${srcdir}"/build-${MINGW_CHOST}  && cd "${srcdir}"/build-${MINGW_CHOST}

  export VULKAN_SDK=${MINGW_PREFIX}
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_BUILD_TYPE="Release" \
    -DDYNAMIC_LOADER=ON \
    -DBUILD_WITH_SYSTEM_JSONCPP=ON \
    ../OpenXR-SDK-release-${pkgver}

  make
}

package() {
  cd ${srcdir}/build-${MINGW_CHOST}
  make DESTDIR="${pkgdir}" install

  if [ "${CARCH}" = "i686" ]; then
    rm -f ${pkgdir}${MINGW_PREFIX}/lib/libopenxr_loader.dll.a
    ${MINGW_PREFIX}/bin/gendef ${pkgdir}${MINGW_PREFIX}/bin/libopenxr_loader.dll
    ${MINGW_PREFIX}/bin/dlltool -k -d libopenxr_loader.def -l ${pkgdir}${MINGW_PREFIX}/lib/libopenxr_loader.dll.a
  fi

  install -Dm644 "${srcdir}"/OpenXR-SDK-release-${pkgver}/LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
